package com.azzcs.juc.semaphore;

import java.util.Random;
import java.util.concurrent.Semaphore;

/**
 * @Author: wzg
 * @Date: 2021/6/17 上午11:17
 */
public class Main {
    public static void main(String[] args) throws InterruptedException {
        Semaphore semaphore = new Semaphore(3);
        Random random = new Random();
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                try {
                    semaphore.acquire();
                    System.out.println(Thread.currentThread().getName()+" is run...");
                    Thread.sleep(random.nextInt(1000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    semaphore.release();
                }
            },"thread "+ i+1).start();
        }

        System.out.println("main run...");
    }
}
